package com.jichangxiu.socket.filter;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.jichangxiu.common.constant.Constants;
import com.jichangxiu.socket.entity.SocketResult;
import lombok.extern.slf4j.Slf4j;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.PerRequestBroadcastFilter;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class SocketFilter implements PerRequestBroadcastFilter {

    @Override
    public BroadcastAction filter(String broadcasterId, Object originalMessage, Object message) {
        return new BroadcastAction(message);
    }

    @Override
    public BroadcastAction filter(String broadcasterId, AtmosphereResource r, Object originalMessage, Object message) {
        SocketResult socketResult = JSONUtil.toBean((String) message, SocketResult.class);
        String userId = getUserIdFromRequest(r.getRequest());
        // 如果收到的消息不是 SocketResult 并且当前没有登录用户则忽略消息
        if (ObjectUtil.isEmpty(socketResult) || ObjectUtil.isEmpty(userId)) {
            if (ObjectUtil.isEmpty(socketResult))
                log.error("收到一条不规范消息：" + message);
            else if (ObjectUtil.isEmpty(userId))
                log.error("当前没有登录用户拒收消息：" + JSONUtil.toJsonStr(socketResult));
            // 忽略这条消息
            return new BroadcastAction(BroadcastAction.ACTION.ABORT, JSONUtil.toJsonStr(socketResult));
        }
        // 没有 发送方 或者 处理页面 的消息一律忽略
        else if (StrUtil.isEmpty(socketResult.getFrom()) || StrUtil.isEmpty(socketResult.getPage())) {
            if (StrUtil.isEmpty(socketResult.getFrom()))
                log.error("接收到没有发送方的消息：" + JSONUtil.toJsonStr(socketResult));
            else if (StrUtil.isEmpty(socketResult.getPage()))
                log.error("接收到没有处理页面的消息：" + JSONUtil.toJsonStr(socketResult));
            // 忽略这条消息
            return new BroadcastAction(BroadcastAction.ACTION.ABORT, JSONUtil.toJsonStr(socketResult));
        } else {
            // 如果 to 是空的则代表发给所有人
            // 消息接收人是当前登录用户
            if (StrUtil.isEmpty(socketResult.getTo()) || ObjectUtil.equals(socketResult.getTo(), userId)) {
                // 接收消息
                return new BroadcastAction(BroadcastAction.ACTION.CONTINUE, JSONUtil.toJsonStr(socketResult));
            } else {
                // 消息接收人不是当前登录用户
                // 忽略这条消息
                return new BroadcastAction(BroadcastAction.ACTION.ABORT, JSONUtil.toJsonStr(socketResult));
            }
        }
    }

    private String getUserIdFromRequest(HttpServletRequest request) {
        String userId = null;
        if (ObjectUtil.isNotEmpty(request)) {
            // 从请求头获取
            userId = request.getHeader(Constants.JCX_PROPERTIES.getServerProperties().getUserIdKeyToServerKey());
            if (StrUtil.isEmpty(userId))
                // 如果从请求头没有获取到有效 userId 那么则从请求参数中获取
                userId = request.getParameter(Constants.JCX_PROPERTIES.getServerProperties().getUserIdKeyToServerKey());
        }
        return userId;
    }

}
